package com.ziki.wicketcms.admin.login;

import org.apache.wicket.markup.head.filter.HeaderResponseContainer;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import com.ziki.bswicket.action.ClickAction;
import com.ziki.bswicket.component.BsButton;
import com.ziki.bswicket.component.BsCheckBox;
import com.ziki.bswicket.component.BsForm;
import com.ziki.bswicket.component.BsPasswordTextField;
import com.ziki.bswicket.component.BsTextField;
import com.ziki.bswicket.page.BsPage;
import com.ziki.wicketcms.admin.login.model.LoginModel;
import com.ziki.wicketcms.admin.welcome.WelcomePage;

public class LoginPage extends BsPage {
    private static final long serialVersionUID = 1L;
    private BsForm<LoginModel> form;

    public LoginPage() {
        initPage();
    }

    public LoginPage(IModel<?> model) {
        super(model);
        initPage();
    }

    public LoginPage(PageParameters parameters) {
        super(parameters);
        initPage();
    }

    private void initPage() {
        addCss("res/bootstrap/css/bootstrap.css");
        addCss("res/font-awesome/css/font-awesome.css");
        addCss("res/jquery-ui/themes/base/jquery.ui.base.css");
        addCss("res/font-awesome/css/font-awesome-ie7.css", "IE 7");
        addCss("res/google/css/font.css");
        addCss("res/custom/css/ace.min.css");
        addCss("res/custom/css/ace-rtl.min.css");
        addCss("res/custom/css/ace-ie.min.css", "lte IE 8");
        addCss("res/custom/css/gims.css");
        addJavaScript("res/custom/js/html5shiv.js", "lt IE 9");
        addJavaScript("res/custom/js/respond.min.js", "lt IE 9");
        // a container for all collected JavaScript contributions that
        // will be loaded at the page footer (after </body>)
        add(new HeaderResponseContainer("footerJS", "footerJS"));

        CompoundPropertyModel<LoginModel> model = new CompoundPropertyModel<>(new LoginModel());
        form = new BsForm<LoginModel>("form", model);
        this.add(form);
        form.add(new BsTextField<String>("userName"));
        form.add(new BsPasswordTextField("password"));
        form.add(new BsCheckBox("isRemember"));
        BsButton btnSubmit = new BsButton("btnSubmit");
        btnSubmit.add(this.getSubmitAction(form));
        form.add(btnSubmit);
    }

    private ClickAction getSubmitAction(final BsForm<LoginModel> form) {
        return new ClickAction() {
            @Override
            public void performClick() {
                LoginModel model = form.getModelObject();
                if (model.isRemember()) {
                    System.out.println("remember me!!");
                }
                System.out.println(model);
                form.setResponsePage(WelcomePage.class);
            }
        };
    }
}
